package org.example.springoauth2.securitycommon.config;

import cn.hutool.core.util.StrUtil;
import org.example.springoauth2.securitycommon.common.BaseException;
import org.example.springoauth2.securitycommon.common.Status;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author hzq
 * @date 2021/10/14 10:46
 */
public class GatewayHeaderFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String header = request.getHeader("GATEWAY-HEADER-KEY");
        if (!StrUtil.equals("gateway-header-secret", header)) {
            throw new BaseException(Status.BAD_GATEWAY);
        }

        filterChain.doFilter(request, response);
    }
}
